using System.Collections;
using System.Collections.Generic;
using PureStateMachine;
using Unity.VisualScripting;
using UnityEngine;

public class LevelManager : MonoBehaviour
{
    public static LevelManager instance;

    public int level;

    private PStateBrain brain;
    private bool start;

    public LevelSelector levelSelector;

    public DeckMove deck;

    public Tips tips;
    

    private void Awake()
    {
        /*if (instance)
        {
            Destroy(gameObject);
        }
        else
        {
            instance = this;
        }*/
        instance = this;

        PawnBuilder.CharaParent = charaParent;
    }

    // Start is called before the first frame update
    void Start()
    {
        //instance.GameStart();
    }

    // Update is called once per frame
    void Update()
    {
        if (start)
        {
            brain.Update();
        }
    }

    public void GameStart()
    {
        brain = new TurnStateBrain();

        brain.Blackboard.RegisterData("DeckMove", deck);

        level = 0;

        brain.Init();
        brain.DebugSetting = new PDebugSetting()
        {
            enable = true,
            updateDisplay = false
        };
        Debug.Log(brain.GetBrainInfo());

        brain.OnDebug += Debug.Log;

        start = true;
    }

    public void GameEnd()
    {
        // the machine will stop immediatly!
        start = false;
    }

    public GameObject PlaceOverButton;

    /// <summary>
    /// Place阶段按按钮
    /// </summary>
    public void OnPlaceOverButton()
    {
        if(!GameManager.instance.ifHeroMode)
        {
            if(GridManager.instance.GetGridNode(1,1).Pawn == null)
            {
                tips.Show();
                return;
            }
        }
        PlaceOverButton.SetActive(false);
        ifCanDrag = false;
        (brain as TurnStateBrain).PlaceState.OnPlaceOver();
    }

    public void OnPlaceStart()
    {
        level++;
        PlaceOverButton.SetActive(true);
        var levelData = levelSelector.SelectorLevel(level);
        InitLevel(levelData);
        ifCanDrag = true;
    }

    public bool ifCanDrag = true;

    public bool ifCanCheckPawn = true;

    public void OnStartChoose()
    {
        ifCanCheckPawn = false;
        GridManager.instance.Clear();
        CardManager.instance.StartChooseNewPawn();
    }

    public void OnChooseEndButton()
    {
        (brain as TurnStateBrain).ChooseState.OnChooseEnd();
        ifCanCheckPawn = true;
    }

    /// <summary>
    /// 2.单位死亡时调用一次 检查是否结束
    /// </summary>
    public void OnPawnDeadCheckIfEnd()
    {
        StopAllCoroutines();
        StartCoroutine(WaitForEndCheck());
    }

    IEnumerator WaitForEndCheck()
    {
        yield return new WaitForSeconds(1.5f);
        (brain as TurnStateBrain).BattleState.CheckIfBattleEnd();
    }

    public Transform charaParent;

    public void InitLevel(LevelData data)
    {
        var cards = data.pawns;
        for (int i = 0; i < cards.Count; i++)
        {
            if (cards[i] == null)
                continue;

            int computerGridIndex = 13 - i;
            Pawn pawn = PawnBuilder.InitPawnByPrefab(cards[i]);
            if (GridManager.instance.grids[computerGridIndex].Pawn != null)
                continue;

            GridManager.instance.grids[computerGridIndex].Pawn = pawn;
            pawn.gridNode = GridManager.instance.grids[computerGridIndex];

            pawn.GetComponent<PawnAnchor>().AnchorPosition
                = GridManager.instance.grids[computerGridIndex].GetComponent<CardPosition>();
            pawn.transform.position = pawn.gridNode.transform.position;

            pawn.campIsSelf = false;
        }
    }
    public GameObject fall;
    public void OnFall()
    {
        fall.SetActive(true);
    }
}
